#include <stdio.h>
#include <pthread.h>

pthread_mutex_t lock1 ;
pthread_mutex_t lock2 ; 


void* task1(void *p)
{
	pthread_mutex_lock(&lock1);
	printf("task1 : thread 1 lock\n");
	usleep(100000);	

	pthread_mutex_lock(&lock2);
	printf("task1 : thread 2 lock\n");	
	pthread_mutex_unlock(&lock2);
	

	pthread_mutex_unlock(&lock1);
}

void* task2(void *p)
{
	pthread_mutex_lock(&lock2);
	printf("task2 : thread 2 lock\n");
	usleep(100000);	

	pthread_mutex_lock(&lock1);
	printf("task2 : thread 1 lock\n");	
	pthread_mutex_unlock(&lock1);
	

	pthread_mutex_unlock(&lock2);
}

int main()
{
	pthread_mutex_init(&lock1,0); 
	pthread_mutex_init(&lock2,0); 
	pthread_t id1,id2;
	pthread_create(&id1 , 0 , task1 ,0 );
	pthread_create(&id2 , 0 , task2 , 0);
	pthread_join(id1,0);
	pthread_join(id2,0);

	pthread_mutex_destroy(&lock1);
	pthread_mutex_destroy(&lock2);
}
